package com.tinem.platform.module.starter.sdk;

import cn.hutool.core.util.StrUtil;
import lombok.Getter;

import java.util.*;

/**
 * @author fzh
 * @version v1
 * @program: platform
 * @className MessageCode
 * @description code 枚举分段
 *                   下划线【_】  为分段标志
 *                   中划先【-】  为单词标志
 *                   第一段【SUCCESS，ERROR】
 *                      SUCCESS 代表成功
 *                      ERROR 代表失败
 *                   第二段【模块名称】
 *                      例如： PLATFORM-CLOUD-SERVICE-LINK
 *                   第三段【业务】
 *                   第四段【错误原因】
 * @site
 * @company
 * @create 2021-06-26 12:24
 */
@Getter
public class MessageCode {
    public final static List<MessageCode> MESSAGE_CODE_LIST = new ArrayList<>();

    /** 成功 */
    public final static MessageCode SUCCESS = new MessageCode("SUCCESS",new HashMap<Locale,String>(){{
        put(Locale.US,"success");
        put(Locale.SIMPLIFIED_CHINESE,"成功");
    }});

    /** 未知错误 */
    public final static MessageCode ERROR_COMMONS_UNKNOWN = new MessageCode("ERROR_COMMONS_UNKNOWN",new HashMap<Locale,String>(){{
        put(Locale.US,"system error:{0}");
        put(Locale.SIMPLIFIED_CHINESE,"系统错误:{0}");
    }});
    public final static MessageCode ERROR_COMMONS_CLIENT_TIME = new MessageCode("ERROR_COMMONS_CLIENT_TIME",new HashMap<Locale,String>(){{
        put(Locale.US,"Client time error: {0}; Server time: {1}");
        put(Locale.SIMPLIFIED_CHINESE,"客户端时间错误:{0}; 服务器时间:{1}");
    }});
    /** 参数验证失败 */
    public final static MessageCode ERROR_COMMONS_REQUEST_REPEAT = new MessageCode("ERROR_COMMONS_REQUEST_REPEAT",new HashMap<Locale,String>(){{
        put(Locale.US,"Duplicate request:{0}");
        put(Locale.SIMPLIFIED_CHINESE,"请求重复:{0}");
    }});
    /** 参数验证失败 */
    public final static MessageCode ERROR_COMMONS_PARAMS = new MessageCode("ERROR_COMMONS_PARAMS",new HashMap<Locale,String>(){{
        put(Locale.US,"parameter validation failed:[{0}]");
        put(Locale.SIMPLIFIED_CHINESE,"参数验证失败:[{0}]");
    }});
    /** 参数验证失败 */
    public final static MessageCode ERROR_COMMONS_PARAMS_FIELD = new MessageCode("ERROR_COMMONS_PARAMS_FIELD",new HashMap<Locale,String>(){{
        put(Locale.US,"{0}");
        put(Locale.SIMPLIFIED_CHINESE,"{0}");
    }});
    /** 参数验证失败 */
    public final static MessageCode ERROR_COMMONS_SERVER = new MessageCode("ERROR_COMMONS_SERVER",new HashMap<Locale,String>(){{
        put(Locale.US,"server error:[{0}]");
        put(Locale.SIMPLIFIED_CHINESE,"服务异常:[{0}]");
    }});
    public final static MessageCode ERROR_COMMONS_ENCRYPTION_TYPE = new MessageCode("ERROR_COMMONS_ENCRYPTION_TYPE",new HashMap<Locale,String>(){{
        put(Locale.US,"success");
        put(Locale.SIMPLIFIED_CHINESE,"加密类型错误，不识别的加密类型:{0}");
    }});
    // -------------------------------------------------platform-cloud-service-user-------------------------------------
    public final static MessageCode ERROR_PLATFORM_CLOUD_SERVICE_USER_MOBILE = new MessageCode("ERROR_PLATFORM_CLOUD_SERVICE_USER_MOBILE",new HashMap<Locale,String>(){{
        put(Locale.US,"The mobile phone has been bound to other accounts");
        put(Locale.SIMPLIFIED_CHINESE,"手机已绑定其他账号");
    }});
    public final static MessageCode ERROR_PLATFORM_CLOUD_SERVICE_USER_EMAIL = new MessageCode("ERROR_PLATFORM_CLOUD_SERVICE_USER_EMAIL",new HashMap<Locale,String>(){{
        put(Locale.US,"Email has been bound to other accounts");
        put(Locale.SIMPLIFIED_CHINESE,"邮箱已绑定其他账号");
    }});
    public final static MessageCode ERROR_PLATFORM_CLOUD_SERVICE_USER_PASSWORD = new MessageCode("ERROR_PLATFORM_CLOUD_SERVICE_USER_PASSWORD",new HashMap<Locale,String>(){{
        put(Locale.US,"Password incorrect");
        put(Locale.SIMPLIFIED_CHINESE,"密码错误");
    }});
    /** 未知错误 */
    public final static MessageCode ERROR_SESSION_NOT_FIND = new MessageCode("ERROR_SESSION_NOT_FIND",new HashMap<Locale,String>(){{
        put(Locale.US,"session not find");
        put(Locale.SIMPLIFIED_CHINESE,"会话不存在，请确认请求参数！");
    }});
    public final static MessageCode ERROR_SESSION_NOT_AUTHORIZE = new MessageCode("ERROR_SESSION_NOT_AUTHORIZE",new HashMap<Locale,String>(){{
        put(Locale.US,"Access to unauthorized url");
        put(Locale.SIMPLIFIED_CHINESE,"访问未授权url");
    }});
    public final static MessageCode ERROR_COUNTRY_BLACK_LIST = new MessageCode("ERROR_COUNTRY_BLACK_LIST",new HashMap<Locale,String>(){{
        put(Locale.US,"Your country has not opened services yet, please look forward to it!");
        put(Locale.SIMPLIFIED_CHINESE,"您所在的国家暂未开放服务，敬请期待!");
    }});
    // -------------------------------------------------platform-cloud-service-pay-------------------------------------
    public final static MessageCode ERROR_PLATFORM_CLOUD_SERVICE_ORDER_NOT_FIND = new MessageCode("ERROR_PLATFORM_CLOUD_SERVICE_ORDER_NOT_FIND",new HashMap<Locale,String>(){{
        put(Locale.US,"order id:{0},is not find.");
        put(Locale.SIMPLIFIED_CHINESE,"找到不订单id：{0}.");
    }});
    public final static MessageCode ERROR_PLATFORM_CLOUD_SERVICE_ORDER_STATUS = new MessageCode("ERROR_PLATFORM_CLOUD_SERVICE_ORDER_STATUS",new HashMap<Locale,String>(){{
        put(Locale.US,"order id:{0},status:{1} is error.");
        put(Locale.SIMPLIFIED_CHINESE,"订单{}状态{}错误.");
    }});
    public final static MessageCode ERROR_PLATFORM_CLOUD_SERVICE_ORDER_EXPIRE = new MessageCode("ERROR_PLATFORM_CLOUD_SERVICE_ORDER_EXPIRE",new HashMap<Locale,String>(){{
        put(Locale.US,"order id:{0},status:{1} is expire.");
        put(Locale.SIMPLIFIED_CHINESE,"订单{}状态{}过期.");
    }});
    public final static MessageCode ERROR_PLATFORM_CLOUD_SERVICE_ORDER_PAY_ERROR = new MessageCode("ERROR_PLATFORM_CLOUD_SERVICE_ORDER_PAY_ERROR",new HashMap<Locale,String>(){{
        put(Locale.US,"order id:{0},pay:{1} is error.");
        put(Locale.SIMPLIFIED_CHINESE,"订单{}支付失败，原因：{1}.");
    }});
    public final static MessageCode ERROR_PLATFORM_CLOUD_SERVICE_ORDER_PAY_CHANNEL_NOT_FIND = new MessageCode("ERROR_PLATFORM_CLOUD_SERVICE_ORDER_PAY_CHANNEL_NOT_FIND",new HashMap<Locale,String>(){{
        put(Locale.US,"order pay channel:{0},is not find.");
        put(Locale.SIMPLIFIED_CHINESE,"找不到订单支付渠道：{0}.");
    }});
    // -------------------------------------------------platform-cloud-web-gateway--------------------------------------
    public final static MessageCode ERROR_PLATFORM_CLOUD_WEB_GATEWAY_CLIENT_NOT_FIND = new MessageCode("ERROR_PLATFORM_CLOUD_WEB_GATEWAY_CLIENT_NOT_FIND",new HashMap<Locale,String>(){{
        put(Locale.US,"403 forbidden client:[{0}]");
        put(Locale.SIMPLIFIED_CHINESE,"客户端id错误:{0}");
    }});
    public final static MessageCode ERROR_PLATFORM_CLOUD_WEB_GATEWAY_SCOPE_NOT_FIND = new MessageCode("ERROR_PLATFORM_CLOUD_WEB_GATEWAY_SCOPE_NOT_FIND",new HashMap<Locale,String>(){{
        put(Locale.US,"403 forbidden scope:[{0}]");
        put(Locale.SIMPLIFIED_CHINESE,"客户端权限不足，无法访问:[{0}]");
    }});
    public final static MessageCode ERROR_PLATFORM_CLOUD_WEB_GATEWAY_SIGN_TYPE = new MessageCode("ERROR_PLATFORM_CLOUD_WEB_GATEWAY_SIGN_TYPE",new HashMap<Locale,String>(){{
        put(Locale.US,"Request signature type mismatch");
        put(Locale.SIMPLIFIED_CHINESE,"请求签名类型不匹配.客户端需要签名类型:{0}; 上传签名类型: {1}");
    }});
    public final static MessageCode ERROR_PLATFORM_CLOUD_WEB_GATEWAY_SIGN = new MessageCode("ERROR_PLATFORM_CLOUD_WEB_GATEWAY_SIGN",new HashMap<Locale,String>(){{
        put(Locale.US,"Request signature mismatch");
        put(Locale.SIMPLIFIED_CHINESE,"请求签名不匹配.");
    }});
    public final static MessageCode ERROR_PLATFORM_CLOUD_WEB_GATEWAY_CRYPTO_TYPE = new MessageCode("ERROR_PLATFORM_CLOUD_WEB_GATEWAY_CRYPTO_TYPE",new HashMap<Locale,String>(){{
        put(Locale.US,"The requested encryption type does not match,{},{}");
        put(Locale.SIMPLIFIED_CHINESE,"请求加密类型不匹配.客户端需要加密类型:{0}; 上传加密类型: {1}");
    }});

    // -------------------------------------------------platform-cloud-web-auth-----------------------------------------

    public final static MessageCode VERIFICATION_CODE_VALID_FOR_FIVE_MINUTES = new MessageCode("VERIFICATION_CODE_VALID_FOR_FIVE_MINUTES",new HashMap<Locale,String>(){{
        put(Locale.US,"The email verification code is valid for {0} minutes. Do not apply for multiple verification codes");
        put(Locale.SIMPLIFIED_CHINESE,"邮箱验证码{0}分钟有效，请勿多次发送");
    }});

    public final static MessageCode ERROR_PLATFORM_CLOUD_WEB_AUTH_CODE = new MessageCode("ERROR_PLATFORM_CLOUD_WEB_AUTH_CODE",new HashMap<Locale,String>(){{
        put(Locale.US,"Mailbox verification code error");
        put(Locale.SIMPLIFIED_CHINESE,"邮箱验证码错误");
    }});
    public final static MessageCode THE_GRAPHIC_VERIFICATION_CODE_IS_INCORRECT = new MessageCode("THE_GRAPHIC_VERIFICATION_CODE_IS_INCORRECT",new HashMap<Locale,String>(){{
        put(Locale.US,"Graphic verification code error");
        put(Locale.SIMPLIFIED_CHINESE,"图形验证码错误");
    }});
    public final static MessageCode ERROR_PLATFORM_CLOUD_WEB_AUTH_CODE_IMAGE = new MessageCode("ERROR_PLATFORM_CLOUD_WEB_AUTH_CODE_IMAGE",new HashMap<Locale,String>(){{
        put(Locale.US,"Please fill in the picture verification code");
        put(Locale.SIMPLIFIED_CHINESE,"请填写图片验证码");
    }});
    public final static MessageCode ERROR_PLATFORM_CLOUD_WEB_AUTH_CODE_TO = new MessageCode("ERROR_PLATFORM_CLOUD_WEB_AUTH_CODE_TO",new HashMap<Locale,String>(){{
        put(Locale.US,"Unrecognized receiver, please enter email or mobile phone number");
        put(Locale.SIMPLIFIED_CHINESE,"不能识别的接收方，请输入邮箱或者手机号");
    }});
    public final static MessageCode ERROR_PLATFORM_CLOUD_WEB_AUTH_CLIENT_IP = new MessageCode("ERROR_PLATFORM_CLOUD_WEB_AUTH_CODE_TO",new HashMap<Locale,String>(){{
        put(Locale.US,"Client IP is not in the whitelist");
        put(Locale.SIMPLIFIED_CHINESE,"客户端IP不在白名单");
    }});

    // -------------------------------------------------platform-cloud-web-websocket------------------------------------
    public final static MessageCode ERROR_PLATFORM_CLOUD_WEB_WEBSOCKET_SESSION_NOT_FIND = new MessageCode("ERROR_PLATFORM_CLOUD_WEB_WEBSOCKET_SESSION_NOT_FIND",new HashMap<Locale,String>(){{
        put(Locale.US,"success");
        put(Locale.SIMPLIFIED_CHINESE,"未找到session:{0}");
    }});
    public final static MessageCode ERROR_PLATFORM_CLOUD_WEB_WEBSOCKET_CONNECTION_STATUS_IS_NOT_CONNECTION = new MessageCode("ERROR_PLATFORM_CLOUD_WEB_WEBSOCKET_CONNECTION_STATUS_IS_NOT_CONNECTION",new HashMap<Locale,String>(){{
        put(Locale.US,"success");
        put(Locale.SIMPLIFIED_CHINESE,"链接未初始化");
    }});

    // -------------------------------------------------platform-cloud-web-file------------------------------------

    public final static MessageCode ERROR_PLATFORM_CLOUD_WEB_FILE_GENERATION_FAILED = new MessageCode("ERROR_PLATFORM_CLOUD_WEB_FILE_GENERATION_FAILED",new HashMap<Locale,String>(){{
        put(Locale.US,"File generation failed");
        put(Locale.SIMPLIFIED_CHINESE,"文件生成失败");
    }});
    public final static MessageCode ERROR_PLATFORM_CLOUD_WEB_FILE_SIZE_LIMIT = new MessageCode("ERROR_PLATFORM_CLOUD_WEB_FILE_SIZE_LIMIT",new HashMap<Locale,String>(){{
        put(Locale.US,"File generation failed");
        put(Locale.SIMPLIFIED_CHINESE,"当前文件大小超过限制");
    }});
    public final static MessageCode ERROR_PLATFORM_CLOUD_WEB_STORAGE_SIZE_LIMIT = new MessageCode("ERROR_PLATFORM_CLOUD_WEB_STORAGE_SIZE_LIMIT",new HashMap<Locale,String>(){{
        put(Locale.US,"File generation failed");
        put(Locale.SIMPLIFIED_CHINESE,"渠道存储空间不足");
    }});

    String name;
    Map<Locale,String> localMessage;

    public MessageCode(String name,Map<Locale,String> localMessage){
        this.name = name;
        this.localMessage = localMessage;
        MESSAGE_CODE_LIST.add(this);
    }

    public static List<MessageCode> values(){
        return MESSAGE_CODE_LIST;
    }
    public static MessageCode valueOf(String name){
        Optional<MessageCode> messageCode = MESSAGE_CODE_LIST.stream().filter(mc -> mc.name().equalsIgnoreCase(name)).findFirst();
        if(messageCode.isPresent()){
            return messageCode.get();
        }
        throw new IllegalArgumentException("No enum "+name);
    }

    public String name(){return name;};


    public String getMessage(Locale locale,Object... params){
        if(locale == null){
            locale = Locale.US;
        }
        return StrUtil.indexedFormat(localMessage.containsKey(locale) ? localMessage.get(locale) : localMessage.get(Locale.US),params);
    }
    public String getMessage(Object... params){
        return getMessage(Locale.US,params);
    }

}
